--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/8/28
-- Time: 17:46
-- To change this template use File | Settings | File Templates.
--

local ScrollText = {}

function ScrollText:create(max) 
end

function UIFlowerFish:ctor()
    self.rootWidget  = cc.CSLoader:createWidget("UI/flowerFish.csb");
    --获取scrollview控件：
    local scroll_view = ccui.Helper:seekWidgetByName(self.rootWidget,"ScrollView_1");
    --设置scollview的滚动区域：
    scroll_view:setInnerContainerSize(cc.size(200,170*12))
    --设置滚动区域的图片（分别是数字0，9，8，7，6，5，4，3，2，1，0，9）
    for i =1 , 4 do
        for n=-1,10 do
            if n == 10 then
                local sp = cc.Sprite:create("UI/Image/slot/roll0.png")
                sp:setPosition(100,170*12-(n+1)*170)
                scroll_view:addChild(sp)
            elseif n == -1 then
                local sp = cc.Sprite:create("UI/Image/slot/roll9.png")
                sp:setPosition(100,170*12-(n+1)*170)
                scroll_view:addChild(sp)
            else
                local sp = cc.Sprite:create("UI/Image/slot/roll"..n..".png")
                sp:setPosition(100,170*12-(n+1)*170)
                scroll_view:addChild(sp)


            end
        end
        scroll_view:jumpToBottom()
        self:slotRun(i,scroll_view,0.01 ,1)
    end
    self.rate_nums = {2,5,4,9}; --代表四个转盘需要停下时代表的数字
    self.Percent_Num = {20,30,40,50,60,70,80,90,100}--设置scrollview停下的位置百分比，假设这几个百分比分别代表0，1，2，3，4，5，6，7，8，9
    self.Percent_Num[0] = 10;
end

function UIFlowerFish:slotRun(tag,slot_scroll,time,round)


    local percent = 0
    local m_round = 0
    local function run()

        percent = percent + 5

        local index = 100-percent;

        slot_scroll:jumpToPercentVertical(100-percent)
        if percent == 100 then
            percent = 0
            m_round=m_round+1
        end
        if m_round<round then  --至少翻滚一圈
            return;
        end
        if  index>=self.Percent_Num[self.rate_nums[tag]]  then --转盘停下的位置的百分比
            slot_scroll:scrollToPercentVertical(80,(10-self.rate_nums[tag])*0.1,false) --scrollview1秒滚到80%的位置
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.waitHandler[tag])--停止计时器
            self.waitHandler[tag] = nil;
        end

    end

    self.waitHandler[tag] = cc.Director:getInstance():getScheduler():scheduleScriptFunc(run, 0.02, false) --启动定时器


end

